﻿package com.ppemi.upload{

	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.net.FileReference;
	import flash.net.FileFilter;
	import flash.display.Sprite;
	import flash.events.*;
	import fl.controls.*;
	import flash.external.ExternalInterface;

	public class Pupload extends Sprite {
		private var _UPLOAD_URL:String=Pconstant.UPLOAD_URL;

		private var fr:FileReference=null;
		private var _mt:TextInput=null;
		private var _mpb:ProgressBar=null;
		private var _msbtn:Button=null;

		private var _mname:String="";
		private var _ftype:String="image";
		private var _utype:String="select";//select,select_upload
		private var _mstatus:String="select";

		private var _onSelected:Function=null;
		private var _onPreUpload:Function=null;
		private var _onUploading:Function=null;
		private var _onUploaded:Function=null;

		//
		public function Pupload() {
			_fr_init();

			//
			ExternalInterface.addCallback("pupload_select", mbrowse);
			ExternalInterface.addCallback("pupload_status", mstatus);
			ExternalInterface.addCallback("pupload_upload", mupload);
		}

		//setter/getter
		public function set MT(mt:TextInput):void {
			this._mt=mt;
		}

		public function set MPB(mpb:ProgressBar):void {
			this._mpb=mpb;
		}

		public function set MSBTN(msbtn:Button):void {
			this._msbtn=msbtn;
		}

		//
		public function mtrigger():void {
			mbrowse();
		}

		public function mbrowse():void {
			fr.browse(this.getTypes("video"));
		}

		public function mupload():void {
			upload();
		}
		public function mstatus():String {
			return this._mstatus;
		}

		public function upload():void {
			if (this._mstatus!="selected") {
				return;
			}

			var vars:URLVariables = new URLVariables();
			var d:Date = new Date();
			vars.time=d.time;
			vars.r=Math.random();

			var ur:URLRequest = new URLRequest();
			ur.url=_UPLOAD_URL;
			ur.method=URLRequestMethod.POST;
			ur.data=vars;
			fr.upload(ur);

			this.call("pvuploadstart");
			this._mstatus="uploading";

			this._msbtn.enabled=false;
			this._mpb.visible=true;
			this._mpb.minimum=0;
			this._mpb.maximum=100;
		}

		//
		private function call(func:String, ... parameters):void {
			if (ExternalInterface.available) {
				ExternalInterface.call(func, parameters);
			}
		}

		//
		private function getTypes(type:String = null):Array {
			var types:Array;
			if (type=="video") {
				types=new Array(this.getVideoTypeFilter());
			} else if ( type == "image" ) {
				types=new Array(this.getImageTypeFilter());
			} else {
				types=new Array(this.getImageTypeFilter(),this.getTextTypeFilter());
			}

			return types;
		}

		private function getImageTypeFilter():FileFilter {
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}

		private function getVideoTypeFilter():FileFilter {
			return new FileFilter("Videos (*.flv, *.mov, *.3gp, *.avi, *.flv, *.mp4, *.wmv)", "*.flv;*.mov;*.3gp;*.avi;*.flv;*.mp4;*.wmv");
		}

		private function getTextTypeFilter():FileFilter {
			return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
		}

		//
		private function _fr_init():void {
			fr = new FileReference();

			fr.addEventListener(Event.SELECT, _fr_select);
			fr.addEventListener(Event.OPEN, _fr_open);
			fr.addEventListener(Event.CANCEL, _fr_cancel);
			fr.addEventListener(Event.COMPLETE, _fr_complete);
			fr.addEventListener(Event.ACTIVATE, _fr_activate);
			fr.addEventListener(Event.DEACTIVATE, _fr_deactivate);
			fr.addEventListener(ProgressEvent.PROGRESS, _fr_progress);
			fr.addEventListener(IOErrorEvent.IO_ERROR, _fr_ioerror);
			fr.addEventListener(HTTPStatusEvent.HTTP_STATUS, _fr_http_status);
			fr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _fr_security_error);
			fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, _fr_upload_complete_data);
		}
		private function _fr_select(ev:Event):void {
			if (_mt) {
				var file:FileReference=FileReference(ev.target);
				_mt.text=file.name;
			}
			this.call("pvselected",fr.name, fr.size);
			this._mstatus="selected";
			if (_utype=="select_upload") {
				upload();
			}

		}
		private function _fr_open(ev:Event):void {

		}
		private function _fr_cancel(ev:Event):void {

		}
		private function _fr_complete(ev:Event):void {

		}
		private function _fr_activate(ev:Event):void {

		}
		private function _fr_deactivate(ev:Event):void {

		}
		private function _fr_progress(ev:ProgressEvent):void {
			this._mpb.setProgress(ev.bytesLoaded, ev.bytesTotal);
			if (ev.bytesLoaded==ev.bytesTotal) {
				this._mpb.visible=false;
			}
		}
		private function _fr_ioerror(ev:IOErrorEvent):void {

		}
		private function _fr_http_status(ev:HTTPStatusEvent):void {

		}
		private function _fr_security_error(ev:SecurityErrorEvent):void {

		}
		private function _fr_upload_complete_data(ev:DataEvent):void {
			this._mstatus="uploaded";
			this.call("pvuploadcomplete", ev.data);
		}

		//setter/getter
		public function set onSelected(f:Function):void {
			_onSelected=f;
		}

		public function set onPreUpload(f:Function):void {
			_onPreUpload=f;
		}

		public function set onUploading(f:Function):void {
			_onUploading=f;
		}

		public function set onUploaded(f:Function):void {
			_onUploaded=f;
		}

	}
}